// Set the module in the RealmConfiguration to allow only classes defined by the module. RealmConfiguration config = new RealmConfiguration.Builder() .modules(new MyModule()) .build();
// It is possible to combine multiple modules to one schema. RealmConfiguration config = new RealmConfiguration.Builder() .modules(new MyModule(), new MyOtherModule()) .build();
// Library must create a module and set library = true. This will prevent the default // module from being created. // allClasses = true can be used instead of listing all classes in the library. @RealmModule(library = true, allClasses = true) publicclassMyLibraryModule{ }
// Library projects are therefore required to explicitly set their own module. RealmConfiguration libraryConfig = new RealmConfiguration.Builder() .name("library.realm") .modules(new MyLibraryModule()) .build();
// Apps can add the library RealmModule to their own schema. RealmConfiguration config = new RealmConfiguration.Builder() .name("app.realm") .modules(Realm.getDefaultModule(), new MyLibraryModule()) .build();
private RealmResults<Dog> puppies; private Dog dog;
@Override protectedvoidonCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); realm = Realm.getDefaultInstance(); puppiesListener = new RealmChangeListener() { @Override publicvoidonChange(RealmResults<Dog> puppies){ // ... do something with the updated puppies instance }};
// Find all the puppies puppies = realm.where(Dog.class).lessThanOrEqualTo("age", 2).findAll(); puppies.addChangeListener(puppiesListener);
dogListener = new RealmChangeListener() { @Override publicvoidonChange(Dog dog){ // ... do something with the updated Dog instance }};
dog = realm.where(Dog.class).equalTo("name", "Fido").findFirst(); dog.addChangeListener(dogListener); }
@Override protectedvoidonDestroy(){ super.onDestroy(); // Remove the listeners puppies.removeChangeListener(puppiesListener); dog.removeChangeListener(dogListener); // Close the Realm instance. realm.close(); } }
你可以轻松移除所有监听器。
1
realm.removeAllChangeListeners();
最后,这些监听器同样会在监听对象的引用对象改变时被触发,请见示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Person person = realm.where(Person.class).findFirst(); person.getDogs(); // => 2 - Assume there are 2 dogs in the list person.addChangeListener(new RealmChangeListener() { @Override publicvoidonChange(Person person){ // React to the change in the Person instance. // This will also get called when any referenced dogs are updated. } }); Dog dog = person.getDogs().get(0); realm.beginTransaction(); dog.setAge(5); realm.commitTransaction(); // Person change listener is called on the next iteration of the run loop because // a referenced dog object changed.
privatefinal OrderedRealmCollectionChangeListener<RealmResults<Person>> changeListener = new OrderedRealmCollectionChangeListener() { @Override publicvoidonChange(RealmResults<Person> collection, OrderedCollectionChangeSet changeSet){ // `null` means the async query returns the first time. if (changeSet == null) { notifyDataSetChanged(); return; } // For deletions, the adapter has to be notified in reverse order. OrderedCollectionChangeSet.Range[] deletions = changeSet.getDeletionRanges(); for (int i = deletions.length - 1; i >= 0; i--) { OrderedCollectionChangeSet.Range range = deletions[i]; notifyItemRangeRemoved(range.startIndex, range.length); }
OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges(); for (OrderedCollectionChangeSet.Range range : insertions) { notifyItemRangeInserted(range.startIndex, range.length); }
OrderedCollectionChangeSet.Range[] modifications = changeSet.getChangeRanges(); for (OrderedCollectionChangeSet.Range range : modifications) { notifyItemRangeChanged(range.startIndex, range.length); } } };